package httpx

import (
	"ci-control-backend/model"
	"github.com/pkg/errors"
)

const __userid = "userid"
const __user = "user"

// SetLoginUserid 保存当前登录用户的ID到session
func (c *Context) SetLoginUserid(userid uint) {
	c.Session.Set(__userid, &userid)
}

// GetLoginUserid 从session获取当前登录用户的ID
func (c *Context) GetLoginUserid() (uint, error) {
	val := c.Session.Get(__userid)
	if val == nil {
		return 0, errors.New("未从session找到用户id")
	}
	userid := val.(*uint)
	return *userid, nil
}

// SetLoginUser 保存当前登录用户到session
func (c *Context) SetLoginUser(user *model.User) {
	c.Session.Set(__user, user)
}

// GetLoginUser 从session获取当前登录用户
func (c *Context) GetLoginUser() (*model.User, error) {
	val := c.Session.Get(__user)
	if val == nil {
		return nil, errors.New("未从session找到用户")
	}
	return val.(*model.User), nil
}
